package com.czh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czh.constants.SystemConstants;
import com.czh.domain.ResponseResult;
import com.czh.domain.entity.User;
import com.czh.domain.vo.CommentPageVo;
import com.czh.domain.vo.CommentVo;
import com.czh.enums.AppHttpCodeEnum;
import com.czh.hander.exception.SystemException;
import com.czh.mapper.CommentMapper;
import com.czh.domain.entity.Comment;
import com.czh.service.CommentService;
import com.czh.service.UserService;
import com.czh.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-04-08 21:57:02
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserService userService;
    @Override
    public ResponseResult<CommentPageVo> getArticleCommentList(Integer articleId, Integer pageNum, Integer pageSize) {

        return ResponseResult.okResult(getCommentVos(articleId,pageNum,pageSize,SystemConstants.ARTICLE_COMMENT_TYPE));
    }
    @Override
    public ResponseResult<CommentPageVo> getLinkCommentList(Integer pageNum, Integer pageSize) {
        return ResponseResult.okResult(getCommentVos(null,pageNum,pageSize,SystemConstants.LINK_COMMENT_TYPE));
    }
    @Override
    public ResponseResult<CommentPageVo> getMessageCommentList(Integer pageNum, Integer pageSize) {
        return ResponseResult.okResult(getCommentVos(null,pageNum,pageSize,SystemConstants.MESSAGE_COMMENT_TYPE));
    }

    @Override
    public ResponseResult<Object> addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.COMMENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> addMessageComment(String msg) {
        save(Comment.builder().
                type(SystemConstants.MESSAGE_COMMENT_TYPE).
                content(msg).
                build());
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> replyMessageComment(Comment comment) {
        comment.setType(SystemConstants.MESSAGE_COMMENT_TYPE);
        save(comment);
        return ResponseResult.okResult();
    }
    /**
     * 评论处理
     */
    private CommentPageVo getCommentVos(Integer articleId, Integer pageNum, Integer pageSize,String type){
        LambdaQueryWrapper<Comment> commentWrapper = new LambdaQueryWrapper<>();
        commentWrapper.eq(articleId!=null,Comment::getArticleId,articleId);
        commentWrapper.eq(Comment::getType,type);
        Page<Comment> commentPage =page(new Page<>(pageNum,pageSize),commentWrapper);

        //给根评论用户名和头像赋值
        List<CommentVo> rootComment = BeanCopyUtils.copyBeanList(commentPage.getRecords(), CommentVo.class).
                stream().map(commentVo -> commentVo.setUsername(userService.getById(commentVo.getCreateBy()).getNickName())).
                map(commentVo -> commentVo.setAvatar(userService.getById(commentVo.getCreateBy()).getAvatar())).
                filter(commentVo -> commentVo.getRootId()==SystemConstants.COMMENT_ROOT_ID).
                collect(Collectors.toList());
        // TODO 这里的子评论要用全部的评论数据，因为commentVos已经被分页了只有前10条数据，而子评论可能在后面
        List<CommentVo> childrenList =  BeanCopyUtils.copyBeanList(list(commentWrapper), CommentVo.class).stream().
                filter(commentVo -> commentVo.getRootId() != SystemConstants.COMMENT_ROOT_ID).
                map(commentVo -> commentVo.setToCommentUserName(userService.getById(commentVo.getToCommentUserId()).getNickName())).
                peek(commentVo ->{
                    User user=userService.getById(commentVo.getCreateBy());
                    commentVo.setUsername(user.getNickName());
                    commentVo.setAvatar(user.getAvatar());
                }).collect(Collectors.toList());
        //将子评论赋值到根评论并生成BeanVo
        return new CommentPageVo(rootComment.stream().map(commentVo ->
                commentVo.setChildren(childrenList.stream().filter(childrenComment ->
                        childrenComment.getRootId()==(commentVo.getId())).collect(Collectors.toList()))).collect(Collectors.toList()),commentPage.getTotal());
    }
}

